<?php

namespace App\Http\Controllers\Api;

use App\Http\Config\ErrorCode;
use App\Models\Admin\AdminUser;
use App\Http\Controllers\Controller;

class BaseController extends Controller
{
    public $noCheckOpenidAction = []; //不校验token的方法名
    public $openid = '';
    public $returnData;

    public function __construct()
    {
        header('Access-Control-Allow-Origin:http://www.wegouer.com');
        if(!is_array($this->noCheckOpenidAction)){
            return $this->result(ErrorCode::$api_enum['params_error'], "noCheckOpenidAction");
        }

        //处理登录openid等信息
        if(!in_array(\request()->route()->getActionMethod(), $this->noCheckOpenidAction) && \request()->post("point_url", "") != "createCustomer"){
            $this->openid = \request()->post("openid");
            if(isset($this->openid)) { //就校验token是否有效或者过期
                $res = AdminUser::from('admin_users as au')
                    ->where('openid','=',$this->openid)
                    ->count();
                if(!$res){
                    return $this->result(ErrorCode::$api_enum['token_expire']);
                }
            }else{
                return $this->result(ErrorCode::$api_enum['params_not_exist'], "openid");
            }
        }

        $this->returnData = ErrorCode::$api_enum['success'];
    }

    public function result($data, $text = '')
    {
        if(strpos($data['msg'], "%s") !== false && $text){
            $data['msg'] = sprintf($data['msg'], $text);
        }
        echo json_encode($data);exit;
    }
}
